package badugaclient;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;


public class GuiStart extends JFrame implements IObservable
{
	private ArrayList<IObserver> observers = new ArrayList<IObserver>();
	private PlayerInfo pInfo;
	private static JTextField hostText, portText;
	private JLabel hostLabel, portLabel; 
	private static JButton connectButton,startButton;
	private boolean changing;
	private static String host = "", portTemp = "";
	private static int port = 0;
	
	public GuiStart (PlayerInfo pInfo)
	{
		super("Badugi");
		this.pInfo = pInfo;
		
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 400, 200);
		getContentPane().setLayout(null);
		
		hostLabel = new JLabel("Host number: ");
		hostLabel.setBounds(10, 10, 150, 30);
		getContentPane().add(hostLabel);
		
		hostText = new JTextField();
		hostText.setBounds(160, 10, 200, 30);
		add(hostText);
		hostText.setColumns(10);
		hostText.setToolTipText("Enter host numbers");

		portLabel = new JLabel("Port number: ");
		portLabel.setBounds(10, 50, 150, 30);
		getContentPane().add(portLabel);
		
		portText = new JTextField();
		portText.setBounds(160, 50, 200, 30);
		add(portText);
		portText.setColumns(10);
		portText.setToolTipText("Enter port numbers");
		
		connectButton = new JButton();
		connectButton.setText("Connect");
		connectButton.setBounds(10, 120, 100, 20);
		add(connectButton);
		 connectButton.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent arg0) 
             {
            	 if(connectButton.getText().equals("Connect"))
            	 {
            		 GuiStart.this.pInfo.setHost(hostText.getText());
            		 portTemp = portText.getText();
            		 if(GuiStart.this.pInfo.getHost().equals(""))
            		 {
            			 GuiStart.this.pInfo.setHost("127.0.0.1");
            		 }
            		 if(GuiStart.this.pInfo.getHost().equals(""))
            		 {
            			 GuiStart.this.pInfo.setConnectionPort(6789);
            		 }
            		 else
            		 {
            			 try
            			 {
            				 GuiStart.this.pInfo.setConnectionPort(Integer.parseInt(portTemp));
            			 }
            			 catch(NumberFormatException ex){}
            		 }
            		 GuiStart.this.pInfo.setContent("Connect");
            		 setChanged();
            		 notifyObservers();
            	 }
            	 else if(connectButton.getText().equals("Disconnect"))
            	 {
            		 GuiStart.this.pInfo.setContent("Disconnect");
            		 setChanged();
            		 notifyObservers();
            	 }
             }
             
           });
		 
		 startButton = new JButton();
		 startButton.setText("Start game");
		 startButton.setBounds(200, 120, 100, 20);
		 startButton.setEnabled(false);
		 add(startButton);
		 startButton.addActionListener(new ActionListener(){
			 public void actionPerformed(ActionEvent arg0) 
			 {
				 GuiStart.this.pInfo.setContent("Start game");
				 startButton.setEnabled(false);
				 String serverMessage = GuiStart.this.pInfo.getMyID() + "RDY__Y";
				 GuiStart.this.pInfo.setSentToServer(serverMessage);
                 setChanged();
                 notifyObservers();


			 }
		 });
	}
	
	public static String getHost()
	{
		return host;
	}
	
	public static void setHost(String host)
	{
		GuiStart.host = host;
	}
	
	public static int getPort()
	{
		return port;
	}
	
	public static void setPort(int port)
	{
		GuiStart.port = port;
	}


	@Override //obserwowany
	public void addObserver(IObserver observer) {
		if(observer != null)
		{
			observers.add(observer);
		}
		
	}

	@Override
	public void removeObserver(IObserver observer) {
	
		observers.remove(observer);
		
	}

	@Override
	public void notifyObservers() {
		if(changing)
		{
			for(IObserver ob : observers)
			{
				ob.updateGUI();
			}
			changing = false;
		}
		
	}

	@Override
	public void setChanged() {
		changing = true;
		
	}
	
	public void UserFailConnection()
	{
		JOptionPane.showMessageDialog(null, "Connection fail");
	}
	
	public void changeToDisconnect()
	{
		connectButton.setText("Disconnect");
	}
	
	public void changeToconnect()
	{
		connectButton.setText("Connect");
	}
	
	public void LetsStartGame()
	{
		connectButton.setEnabled(false);
		startButton.setEnabled(true);

	}

}
